package com.glory.shsi1rpt

import java.text.SimpleDateFormat;

/**
 * @Program: SHSI2RPT
 * @Description: 获取时间工具类
 * @Author: ivan
 */
class DateUtil {

    public static final String YEAR_DATA_PATTERN = "yyyy";
    public static final String MONTH_DATA_PATTERN = "yyyy-MM";
    public static final String DEFAULT_DATA_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DAY_DATA_PATTERN = "yyyy-MM-dd";

    /**
     * 获取开始时间
     * @param vStartTime
     * @return
     */
    static def getStartTime(vStartTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATA_PATTERN)
        def startTime = vStartTime ? sdf.parse(vStartTime) : sdf.parse(sdf.format(new Date().clearTime()))
        startTime = sdf.format(startTime)
        [
                startTime: startTime
        ]
    }

    /**
     * 获取结束时间
     * @param vEndTime
     * @return
     */
    static def getEndTime(vEndTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATA_PATTERN)
        def endTime = vEndTime ? sdf.parse(vEndTime) : sdf.parse(sdf.format(new Date()))
        endTime = sdf.format(endTime)
        [
                endTime: endTime
        ]
    }

    /**
     * 获取开始日期
     * @param vStartDate
     * @return
     */
    static def getStartDate(vStartDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_DATA_PATTERN)
        def startDate = vStartDate ? sdf.parse(vStartDate) : sdf.parse(sdf.format(new Date().clearTime()))
        startDate = sdf.format(startDate)
        [
                startDate: startDate
        ]
    }

    /**
     * 获取结束日期
     * @param vEndDate
     * @return
     */
    static def getEndDate(vEndDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_DATA_PATTERN)
        def endDate = vEndDate ? sdf.parse(vEndDate) : sdf.parse(sdf.format(new Date().clearTime()))
        endDate = sdf.format(endDate)
        [
                endDate: endDate
        ]
    }

    /**
     * 获取当年
     * @param vYearTime
     * @return
     */
    static def getYearTime(vYearTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_DATA_PATTERN)
        def yearTime = vYearTime ? sdf.parse(vYearTime) : sdf.parse(sdf.format(new Date()))
        yearTime = sdf.format(yearTime)
        [
                yearTime: yearTime
        ]
    }

    /**
     * 获取开始当月
     * @param vMonthTime
     * @return
     */
    static def getStartMonthTime(vMonthTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(MONTH_DATA_PATTERN)
        def startMonthTime = vMonthTime ? sdf.parse(vMonthTime) : sdf.parse(sdf.format(new Date()))
        startMonthTime = sdf.format(startMonthTime)
        [
                startMonthTime: startMonthTime
        ]
    }

    /**
     * 获取结束当月
     * @param vMonthTime
     * @return
     */
    static def getEndMonthTime(vMonthTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(MONTH_DATA_PATTERN)
        def endMonthTime = vMonthTime ? sdf.parse(vMonthTime) : sdf.parse(sdf.format(new Date()))
        endMonthTime = sdf.format(endMonthTime)
        [
                endMonthTime: endMonthTime
        ]
    }

    /**
     * 8:30
     * @Author Ivan
     * @Date 2019/8/22
     * @Params
     * @Return
     */
    static def getStartEndTime(vToDayStart, vToDayEnd, vSelectTime, vStartTime, vEndTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATA_PATTERN)
        def todayStart = vToDayStart ? sdf.parse(vSelectTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def todayEnd = vToDayEnd ? sdf.parse(vSelectTime) : sdf.parse(sdf.format(new Date().clearTime()))
        SimpleDateFormat sdf2 = new SimpleDateFormat(DEFAULT_DATA_PATTERN)
        def selectTime = vSelectTime ? sdf2.parse(vSelectTime) : sdf2.parse(sdf2.format(new Date().clearTime()))
        String strDate = sdf.format(new Date())
        vSelectTime = vSelectTime ?: sdf2.format(new Date());
        int strDateH = Integer.parseInt(strDate.substring(11, 13))
        int strDateM = Integer.parseInt(strDate.substring(14, 16))
        if (strDateH <= 8 && strDateM < 30) {
            // 昨天到今天
            Calendar ca1 = Calendar.getInstance()
            ca1.setTime(selectTime)
            ca1.add(Calendar.HOUR_OF_DAY, 8)
            ca1.add(Calendar.MINUTE, 30)
            todayStart = ca1.getTime()
            Calendar ca2 = Calendar.getInstance()
            ca2.setTime(selectTime)
            ca2.add(Calendar.HOUR_OF_DAY, -16)
            ca2.add(Calendar.MINUTE, 30)
            todayEnd = ca2.getTime()
        } else {
            // 今天到明天
            Calendar ca1 = Calendar.getInstance()
            ca1.setTime(selectTime)
            ca1.add(Calendar.HOUR_OF_DAY, 8)
            ca1.add(Calendar.MINUTE, 30)
            todayStart = ca1.getTime()
            Calendar ca2 = Calendar.getInstance()
            ca2.setTime(selectTime)
            ca2.add(Calendar.HOUR_OF_DAY, 32)
            ca2.add(Calendar.MINUTE, 30)
            todayEnd = ca2.getTime()
        }
        SimpleDateFormat sdf3 = new SimpleDateFormat(DEFAULT_DATA_PATTERN)
        def startTime = vStartTime ? sdf3.parse(vStartTime) : sdf3.parse(sdf3.format(todayStart))
        def endTime = vEndTime ? sdf3.parse(vEndTime) : sdf3.parse(sdf3.format(todayEnd))
        startTime = sdf3.format(startTime)
        endTime = sdf3.format(endTime)
        [
                startTime: startTime,
                endTime  : endTime
        ]
    }
}